﻿using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
using DapperExtensions;
using System.Data;
using DapperExtensions.Sql;

namespace Logistics_Dao
{

    public class BidDisputeDao
    {
        private SqlGeneratorImpl sqlGenerator;
        public int BidDisputeAdd(wuliu_bid_dispute model)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Insert<wuliu_bid_dispute>(model);            
            }
        }
        /// <summary>
        /// 新增点评
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public bool AddComment(wuliu_wlcompany_comment mod)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Insert<wuliu_wlcompany_comment>(mod) > 0;
            }
        }

        public object GelBidList(int companyid, int pageIndex, int pageSize)
        {
            string sql = @" from wuliu_bid_base as a LEFT JOIN  wuliu_bid_dispute as b 
                          on a.bid_id=b.bid_id where a.company_id=@companyid  ";

            using (var conn = ConnectionHelper.OpenConnection())
            {
                var pageData = new PageData<DataTable>();
                DynamicParameters dynamicParameters = new DynamicParameters();
                dynamicParameters.Add("companyid", companyid);
                //var whereSql = GetWhereSql(predicate, dynamicParameters);
                //if (!string.IsNullOrEmpty(whereSql))
                //{
                //    sql += " and " + whereSql;
                //}
                var num = conn.ExecuteScalar<int>("select count(1) " + sql, dynamicParameters);

                sql += "  order by a.bid_id DESC limit @pageIndex,@pageSize";
                //dynamicParameters.AddDynamicParams(pickParam);
                dynamicParameters.Add("pageIndex", pageIndex * pageSize);
                dynamicParameters.Add("pageSize", pageSize);
                //var count = conn.Count<wuliu_bid_load>(predicate);
                var data = conn.Query(" SELECT a.bid_id,a.bid_no,a.number,a.end_bid_time,a.consignee_time,a.end_city_name,b.id as id " + sql, dynamicParameters);
                pageData.DataObj = data;
                pageData.TotalNum = num;
                pageData.CurrentPage = pageIndex;
                pageData.TotalPageCount = num % pageSize == 0 ? num / pageSize : num / pageSize + 1;
                return pageData;
            }
        }


        public object GelBidDisputeList(int companyid, int pageIndex, int pageSize)
        {
            string sql = @" from wuliu_bid_base as a LEFT JOIN  wuliu_bid_dispute as b 
                          on a.bid_id=b.bid_id where a.company_id=@companyid  and b.id is not null ";

            using (var conn = ConnectionHelper.OpenConnection())
            {
                var pageData = new PageData<DataTable>();
                DynamicParameters dynamicParameters = new DynamicParameters();
                dynamicParameters.Add("companyid", companyid);
                //var whereSql = GetWhereSql(predicate, dynamicParameters);
                //if (!string.IsNullOrEmpty(whereSql))
                //{
                //    sql += " and " + whereSql;
                //}
                var num = conn.ExecuteScalar<int>("select count(1) " + sql, dynamicParameters);

                sql += "  order by a.bid_id DESC limit @pageIndex,@pageSize";
                //dynamicParameters.AddDynamicParams(pickParam);
                dynamicParameters.Add("pageIndex", pageIndex * pageSize);
                dynamicParameters.Add("pageSize", pageSize);
                //var count = conn.Count<wuliu_bid_load>(predicate);
                var data = conn.Query(" SELECT a.bid_id,a.bid_no,a.number,a.end_bid_time,a.consignee_time,a.end_city_name,b.id as id " + sql, dynamicParameters);
                pageData.DataObj = data;
                pageData.TotalNum = num;
                pageData.CurrentPage = pageIndex;
                pageData.TotalPageCount = num % pageSize == 0 ? num / pageSize : num / pageSize + 1;
                return pageData;
            }
        }

        private string GetWhereSql(IPredicate predicate, DynamicParameters dynamicParameters)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            var wherSql = predicate.GetSql(sqlGenerator, parameters);

            foreach (var parameter in parameters)
            {
                dynamicParameters.Add(parameter.Key, parameter.Value);
            }
            return wherSql;
        }

        /// <summary>
        /// 获取投诉调解详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object BidDisputeGet(int id)
        {
            string sql = @"SELECT * from wuliu_bid_dispute as a LEFT JOIN  wuliu_bid_dispute_result as b 
                          on a.id=b.dispute_id where a.id=@id";
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Query(sql, new { id });
            }
        }
    }
}
